<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>计算数组中出现元素的次数</title>
	</head>
	<body>
		<h4>题目描述</h4>
		<p>统计数组 arr 中值等于 item 的元素出现的次数</p>
		<span>count([1, 2, 4, 4, 3, 4, 3], 4)</span>
		<script type="text/javascript">
			//常规循环方法
			function count(arr, item) {
				var count=0;
			    for(var i=0;i<arr.length;i++){
			        if(arr[i]===item){
			            count++;
			        }
			    }
			    return count;
			}
			//reduce方法
			function count(arr,item){
				var count = arr.reduce(function(prev,curr){
					return curr===item?prev+1:prev;
				},0);
				return count;
			}
			//forEach方法
			function count(arr,item){
				var count=0;
				arr.forEach(function(ele){
					ele === item ?count++:0;
				});
				return count;
			}
		</script>
	</body>
</html>
